<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择知识','error','jy_zhongyi/index/');
	}
	$this->session('id',$id);
	$params=array('uniacid'=>$_SESSION['uniacid'],'show'=>1,'id'=>$id);
	if($_RQ['keyword']){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
	}
	$item=pdo_get('jy_zhongyi_worker_goods',$params);
	if($item['show']!=1){
		exi('该商品项目已经关闭！','error','jy_zhongyi/index/');
	}
	$item['thumb']=tomedia($item['image']);
	$item['createtime']=date('Y-m-d',$item['createtime']);
	$return['worker']=pdo_get('jy_zhongyi_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['workerid']),array('name','is_mingp','id','headimg','mobile','tel','wechat','storeid'));
	$return['store']=pdo_get('jy_zhongyi_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$return['worker']['storeid']),array('title','address','pcd'));
	//更新足迹
	pdo_update('jy_zhongyi_worker_goods',array('readnum +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	//是否关注
	$return['isfollow']=pdo_getcolumn('jy_zhongyi_userlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'workerid'=>$item['workerid']),'isfollow');
	$return['item']=$item;
	$params=array('uniacid'=>$_SESSION['uniacid'],'show'=>1);	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_zhongyi_worker_goods',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	foreach($list as &$kv){
		$item['thumb']=tomedia($item['image']);
	}
	$return['tuijievideos']=$list;
	$return['post']=array('nickname'=>pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'nickname'));
	$return['sharedata']=array(
		'title'=>$item['title']?$item['title']:'商品项目详情',
		'desc'=>$item['desc']?$item['desc']:'商品项目说明',
		'link'=>custom_url('jy_zhongyi/worker_goods//id='.$id,true),
		'imgUrl'=>tomedia($item['image']),
	);
	set_html('title',$item['title']);
	exi($return);
}
if($_RQ['op']=='getlist'){
	$post=get_postdata();
	$params=array('uniacid'=>$_SESSION['uniacid'],'show'=>1,'workerid'=>intval($_RQ['workerid']));
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>array('id','title','image','desc','readnum'),
		'orderby'=>$orderby,
	);
	$list=pdo_getslice('jy_zhongyi_worker_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['image']=tomedia($item['image']);
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='pay'){
	$postdata=get_postdata();
	$goodsid = $_RQ['id'];
	//判断待支付订单及判断是否超过可预订次数
	$goods=pdo_get('jy_zhongyi_worker_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'storeid'=>$goods['storeid'],
			'workerid'=>$goods['workerid'],
			'ordersn'=>get_ordersn('jy_zhongyi_worker_goods_order','ordersn'),
			'goods_id'=>$goodsid,
			'money'=>$goods['price'],
			'times'=>$goods['times'],
			'lasttimes'=>$goods['times'],
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_zhongyi_worker_goods_order',$insert);
	$id=pdo_insertid();
	$params=array(
					'id'=>$id,
					'title'=>'购买医生项目',
					'fee'=>$goods['price'],
					'plugin'=>'jy_zhongyi',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_zhongyi/worker_goods/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_zhongyi/worker_goods.log/',
					'error_url'=>'jy_zhongyi/worker_goods.log/',
	);
	//虚拟设置
	$plugin_xuni=$this->model('model')->plugin_xuni($insert);
	if(count($plugin_xuni)){
		$params['plugin_xuni']=$plugin_xuni;
	}
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_zhongyi_worker_goods_order',array('pay_id'=>$payid),array('id'=>$id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$id,
	);
	exi($res);
}
if($_RQ['op']=='twopay'){
	$id=$_RQ['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_zhongyi_worker_goods_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_zhongyi/worker_goods.log/');
	}
	$order=pdo_get('jy_zhongyi_worker_goods_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_zhongyi/worker_goods.log/');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['money'].'|'.$payresult['fee'],'error','jy_zhongyi/worker_goods.log/');
	}
	pdo_update('jy_zhongyi_worker_goods_order',array('status'=>1,'ispay'=>1,'paytime'=>time(),'paytype'=>$payresult['paytype']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$payresult['id']));
	exi('提交成功!');
}
?>